 /* frmManutencaoDeUsuarios.java
 *
 * Created on 08/11/2011, 10:37:19
 */
package scc.Telas;

import Dao.UserDAO;
import Entidade.Usuario;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JOptionPane;

/**
 *
 * @author Davidson
 */
public class frmCadastrarUsuario extends javax.swing.JInternalFrame {

    private static frmCadastrarUsuario tela1;
    private static Usuario usuario;
    private static UserDAO dao;
    
    /** Creates new form frmManutencaoDeUsuarios */
    private frmCadastrarUsuario() {
        initComponents();        
        lblUsuario.setText(usuario.getNome());        
        this.setVisible(true);
        this.moveToFront();        
    }
    
    public static frmCadastrarUsuario getInstancia(UserDAO dao2, Usuario usuario2){
        if (tela1 == null) {
            dao = dao2;
            usuario = usuario2;
            tela1 = new frmCadastrarUsuario();
        }
        return tela1;
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        lblSenhaAtual = new javax.swing.JLabel();
        lblNovaSenha = new javax.swing.JLabel();
        pwdSenha = new javax.swing.JPasswordField();
        lblNovaSenhaConfirmacao = new javax.swing.JLabel();
        pwdSenhaConfirmacao = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        lblTitulo1 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblSenhaAtual1 = new javax.swing.JLabel();
        txtLogin = new javax.swing.JTextField();
        btCadastrar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setMinimumSize(new java.awt.Dimension(758, 361));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblUsuario.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblUsuario.setForeground(new java.awt.Color(102, 102, 102));

        lblInfo.setText("O Sistema diferencia entre maiúsculo e minúsculo.");

        lblSenhaAtual.setText("Digite o Nome Completo");

        lblNovaSenha.setText("Digite a senha");

        pwdSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        pwdSenha.setMinimumSize(new java.awt.Dimension(149, 25));
        pwdSenha.setPreferredSize(new java.awt.Dimension(149, 25));

        lblNovaSenhaConfirmacao.setText("Confirme a senha");

        pwdSenhaConfirmacao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        pwdSenhaConfirmacao.setMinimumSize(new java.awt.Dimension(149, 25));
        pwdSenhaConfirmacao.setPreferredSize(new java.awt.Dimension(149, 25));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scc/Imagens/User.png"))); // NOI18N

        lblTitulo1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        lblTitulo1.setForeground(new java.awt.Color(0, 51, 153));
        lblTitulo1.setText("Cadastrar novo Usuario");

        txtNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtNome.setMinimumSize(new java.awt.Dimension(217, 25));
        txtNome.setPreferredSize(new java.awt.Dimension(217, 25));

        lblSenhaAtual1.setText("Digite o Login");

        txtLogin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtLogin.setMinimumSize(new java.awt.Dimension(149, 25));
        txtLogin.setPreferredSize(new java.awt.Dimension(149, 25));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(lblUsuario))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblNovaSenhaConfirmacao)
                            .addComponent(pwdSenhaConfirmacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNovaSenha)
                            .addComponent(pwdSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSenhaAtual)
                            .addComponent(lblTitulo1))
                        .addGap(323, 323, 323))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSenhaAtual1)
                            .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblUsuario))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblTitulo1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblSenhaAtual)
                        .addGap(1, 1, 1)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblSenhaAtual1)
                        .addGap(1, 1, 1)
                        .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblNovaSenha)
                        .addGap(1, 1, 1)
                        .addComponent(pwdSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblNovaSenhaConfirmacao)
                        .addGap(1, 1, 1)
                        .addComponent(pwdSenhaConfirmacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblInfo)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btCadastrar.setText("Cadastrar Usuario");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btCadastrar)
                .addGap(18, 18, 18)
                .addComponent(btCancelar)
                .addGap(127, 127, 127))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(btCancelar)
                    .addComponent(btCadastrar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        
        setVisible(false);        
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        
        String nome = txtNome.getText();
        String login = txtLogin.getText();
        String senha = new String(pwdSenha.getPassword());
        String senhaConfirmacao = new String(pwdSenhaConfirmacao.getPassword());
        
        if (nome.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite um Nome!", "Cadastro de Usuário", JOptionPane.ERROR_MESSAGE);
            txtNome.requestFocus();
            return;
        }
        if (login.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite um Login!", "Cadastro de Usuário", JOptionPane.ERROR_MESSAGE);
            txtLogin.requestFocus();
            return;
        }
        
        if (senha.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite uma senha!", "Cadastro de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdSenha.requestFocus();
            return;
        }
        if (senhaConfirmacao.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Confirme a senha!", "Cadastro de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdSenha.requestFocus();            
            return;
        }
        if (!senha.equals(senhaConfirmacao)) {
            JOptionPane.showMessageDialog(rootPane, "As senhas não conferem!", "Cadastro de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdSenha.setText("");
            pwdSenhaConfirmacao.setText("");
            pwdSenha.requestFocus();
            return;
        }
        try {
            dao.addUser(nome, login, senha);
            JOptionPane.showMessageDialog(this.getParent(), "Usuario Cadastrado!");
            txtNome.setText("");
            txtLogin.setText("");
            pwdSenha.setText("");
            pwdSenhaConfirmacao.setText("");
            
        } catch (SQLException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
            Logger.getLogger(frmCadastrarUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }        
        
    }//GEN-LAST:event_btCadastrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCadastrar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblNovaSenha;
    private javax.swing.JLabel lblNovaSenhaConfirmacao;
    private javax.swing.JLabel lblSenhaAtual;
    private javax.swing.JLabel lblSenhaAtual1;
    private javax.swing.JLabel lblTitulo1;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPasswordField pwdSenha;
    private javax.swing.JPasswordField pwdSenhaConfirmacao;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
